package com.ubao.weibo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;

import com.ubao.activity.MyApp;
import com.ubao.activity.MyApp.GoBack;
import com.ubao.network.HttpHandler;
import com.ubao.network.ResponseInterface;
import com.ubao.network.req.SpecialRequest;
import com.ubao.network.resp.SpecialResponse;


public class ShareSinaWeiboActivity extends ShareWeiboActivity {


	public void onCreate(Bundle bundle) {
		
		super.onCreate(bundle);
	}
	
	

	@Override
	protected void initView() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void getIntentData() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setIntentData() {
		// TODO Auto-generated method stub

	}
	
	private void sendRequest(String content){
		String value =content;//;;"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";// mEditText.getText().toString().trim();
	
		String data = WeiboConstant.sendWeiboDataSina(MyApp.getMyApp().weiboUser.sina_token,value);//"format=json&content="+URLEncoder.encode(value.trim())+"&clientip=0&jing=0&wei=0&syncflag=0";
		String url=WeiboConstant.sendWeiboUriSina();
		
		final SpecialRequest request = new SpecialRequest(url);
		request.postParams=data;
		
		request.msg_id = 1001;

		if (mHttpHandler == null) {
			mHttpHandler = new HttpHandler(this, 1000);
		}
		mHttpHandler.sendRequest(request);
	}
	
	@Override
	protected void httpCompleted(ResponseInterface resp) { 
		// TODO Auto-generated method stub
		
//		 {
		
			final SpecialResponse response = (SpecialResponse) resp;
			if(response.msg_id==1001){
				Log.e("content", "text:"+response.content);
				int resCode = mHttpHandler.getHttpClient().getResCode();
				if(resCode!=200){
					String  error= response.content;
					Log.e("error", error);
					
					int offset = error.indexOf("error_code")+12;
					int length = error.substring(offset).indexOf(",");
					int errorCode = Integer.parseInt(error.substring(offset,offset+length));
					Log.e("errorCode", ""+errorCode);
					
					handleError(errorCode);
				}else {
					Message msg =new Message();
					msg.obj="发送成功";
					toastHandler.sendMessage(msg);
					GoBack.goBack(this);
					finish();
					//Toast.makeText(this, "发送成功", Toast.LENGTH_LONG).show();
				}
			}
			
//				
				 
//				Message msg_new = new Message();
//				if (response.content != null
//						&& response.content.trim().equalsIgnoreCase("ok")) {
//					msg_new.arg1 = 1;
//				} else {
//					msg_new.arg1 = -1;
//				}
//			
//		}
	}
private void handleError(int errorCode){
		if(errorCode==21315||errorCode==21327){
			Message msg =new Message();
			msg.obj="token已过期,请重新绑定";
			toastHandler.sendMessage(msg);
			
			MyApp.getMyApp().weiboUser.removeBindSina();
		}else if(errorCode==20019){
			Message msg =new Message();
			msg.obj="重复提交";
			toastHandler.sendMessage(msg);
		}
		else
		{
			Message msg =new Message();
			msg.obj="错误"+errorCode;
			toastHandler.sendMessage(msg);
		}
	}
//@Override
//public void onLeftButtonClick() {
//	// TODO Auto-generated method stub
//	GoBack.goBack(this);
//	
//}
//@Override
//public void onRightButtonClick() {
//	// TODO Auto-generated method stub
//	if(MyApp.getMyApp().weiboUser.isSinaBind()){
//	sendRequest(getSendContent());
//	}else{
//		setGoBack();
//		Intent intent = new Intent(this,
//				SinaWeiboOAuthActivity.class);
//		startActivity(intent);
//	}
//}	
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
	if(keyCode==KeyEvent.KEYCODE_BACK){
		GoBack.goBack(this);
		return true;
	}
	// TODO Auto-generated method stub
	return super.onKeyDown(keyCode, event);
}
@Override
protected void onResume(){
	String temp = null;
	if(MyApp.getMyApp().weiboUser.isSinaBind()){
		temp = MyApp.getMyApp().weiboUser.sina_name;
	}else{
		temp = "未绑定";
	}
	mTextBind.setText(temp);
	super.onResume();
}

}
